package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbCourse;
import com.beetlestech.nfe.main.biz.com.dto.CourseInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.CourseListDTO;
import com.beetlestech.nfe.main.biz.com.dto.CourseTrialDTO;
import com.beetlestech.nfe.main.biz.com.dto.LessonInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.MstcourseInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.SectionInfoDTO;
import com.beetlestech.nfe.main.biz.com.dto.VcourseinfoforteacherInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.CoursesectionDTO;
import com.beetlestech.nfe.main.db.tbl.CoursetrialDTO;
import com.beetlestech.nfe.main.db.tbl.MstcourseDTO;
import com.beetlestech.nfe.main.db.tbl.MstfileDTO;
import com.beetlestech.nfe.main.db.tbl.MstlessonDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.OrderdetailDTO;
import com.beetlestech.nfe.main.db.tbl.SectionlessonDTO;
import com.beetlestech.nfe.main.db.tbl.StudentorderDTO;
import com.beetlestech.nfe.main.db.tbl.StudyhistoryDTO;
import com.beetlestech.nfe.main.db.view.VcourseDTO;
import com.beetlestech.nfe.main.db.view.VorderidfordeletestudentDTO;
import com.beetlestech.nfe.main.db.view.VsectionidfordeleteteacherDTO;
import com.beetlestech.nfe.main.db.view.VsectionlessonDTO;
import com.beetlestech.nfe.main.db.view.VstudylessonDTO;

public class HbCourseImpl extends AbstractDBLogic implements HbCourse, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "课程信息表";

	public CourseInfoDTO getCourse(Integer courseID, Integer orderID){
		CourseInfoDTO courseInfo = getCourseBase(courseID);
		courseInfo.setCourseTrial(getCourseTrial(courseID));
		courseInfo.setSectionInfoArray(getSectionFatherArray(courseID, orderID));
		return courseInfo;
	}
	
	public CourseListDTO getCourseList(Integer rowStar, Integer rowEnd, String starDate, Integer sort, 
			String target, Integer[] selectedCode){
		CourseListDTO courseRecommend = new CourseListDTO();
		List<CourseInfoDTO> courseList = new ArrayList<CourseInfoDTO>();
		
		StatementDTO statementDTO = new StatementDTO();

		if(!StringUtil.isEmpty(starDate)){
			StatementUtil.andWhere(statementDTO, VcourseDTO.COLUMN_COURSESTART, 
					StatementUtil.KEYWORD.GREATER_EQUAL_THAN, starDate);
		}
		
		if(!StringUtil.isEmpty(target)){
			StatementUtil.andWhere(statementDTO, VcourseDTO.COLUMN_NAME, StatementUtil.KEYWORD.LIKE, target);
		}
		
		StringBuffer sbCourse = new StringBuffer();
		
		if(selectedCode!=null){
			sbCourse.append(SQLConstant.BRACKET_LEFT);
			
			StatementDTO tempDTO = new StatementDTO();
			
			for(Integer item: selectedCode){
				StatementUtil.orWhere(tempDTO, VcourseDTO.COLUMN_COURSETYPE_C, StatementUtil.KEYWORD.EQUAL, item);
			}
			
			sbCourse.append(tempDTO.getSQLStatement());
			sbCourse.append(SQLConstant.BRACKET_RIGHT);
		}

		VcourseDTO searchDTO = new VcourseDTO();
		StringBuffer sqlSB = new StringBuffer();
		
		if(sbCourse.length()==0){
			searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		}
		else{
			if(statementDTO.getSQLStatement()!=null)
			{
				sqlSB.append(statementDTO.getSQLStatement());
				sqlSB.append(SQLConstant.SQL_AND);
				
			}
			sqlSB.append(sbCourse);
			searchDTO.set_dynamicWhereCondition(sqlSB.toString());
		}

		if(sort!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(VcourseDTO.COLUMN_COURSESTART);
			if(sort == 0){
				sb.append(" ASC");
			}
			else{
				sb.append(" DESC");
			}
			searchDTO.set_orderByCondition(sb.toString());
		}
		
		courseRecommend.setRecordCount(queryDAO.count(VcourseDTO.COUNT_SEARCH, searchDTO));
		
		StatementUtil.andWhere2(statementDTO, "rowNo", StatementUtil.KEYWORD.GREATER_EQUAL_THAN, rowStar);
		StatementUtil.andWhere2(statementDTO, "rowNo", StatementUtil.KEYWORD.LESS_EQUAL_THAN, rowEnd);
		if(sbCourse.length()>0){
			if(statementDTO.getSQLStatement()!=null){
				sbCourse.insert(0, SQLConstant.SQL_AND);
				searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement() + sbCourse.toString());
			}
			else{
				searchDTO.set_dynamicWhereCondition(sbCourse.toString());
			}
		}
		else{
			searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		}
		
		//
		searchDTO.set_dynamicWhereCondition2(statementDTO.getSQLStatement2());
		
		List<VcourseDTO> courseInfos = queryDAO.queryForObjectList(SQLID.VCOURSE_PAGE_SQL_SEARCH, searchDTO);
		
		for(VcourseDTO item : courseInfos){
			courseList.add(convertToCourseInfo(item));
		}

		courseRecommend.setCourseInfoArray(courseList.toArray(new CourseInfoDTO[courseList.size()]));
		return courseRecommend;
	}
	
	private CourseTrialDTO getCourseTrial(Integer courseID) {
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, CoursetrialDTO.COLUMN_COURSE_ID, StatementUtil.KEYWORD.EQUAL, courseID);
		CoursetrialDTO searchDTO = new CoursetrialDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		CoursetrialDTO trialDB = queryDAO.queryForObject(CoursetrialDTO.SQL_SEARCH, searchDTO);
		
		if(trialDB == null){
			createApplicationException(M10010WS);
		}
		
		CourseTrialDTO courseTrial = new CourseTrialDTO();
		MstlessonDTO mstlesson = new MstlessonDTO();
		mstlesson.setlessonid(trialDB.getlessonid());
		mstlesson = queryDAO.queryForObject(MstlessonDTO.EQUAL_SEARCH, mstlesson);
		
		if(mstlesson == null){
			createApplicationException(M10010WS);
		}
		courseTrial.setName(mstlesson.getname());
		
		MstfileDTO mstfile = new MstfileDTO();
		mstfile.setfileid(mstlesson.getfileid());
		mstfile = queryDAO.queryForObject(MstfileDTO.EQUAL_SEARCH, mstfile);
		
		if(mstfile == null){
			createApplicationException(M10010WS);
		}
		
		courseTrial.setPath(mstfile.getpath());
		
		return courseTrial;
	}

	private SectionInfoDTO convertToSectionInfo(CoursesectionDTO dto, Integer orderID){
		SectionInfoDTO sectionInfo = new SectionInfoDTO();
		sectionInfo.setsectionid(dto.getsectionid());
		sectionInfo.setname(dto.getname());
		setChildrenSectionArray(dto.getcourseid(), dto.getsectionid(), orderID, sectionInfo);
		return sectionInfo;
	}
	
	private void setChildrenSectionArray(Integer courseID, Integer sectionID, Integer orderID, SectionInfoDTO sectionInfo){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, CoursesectionDTO.COLUMN_COURSE_ID, StatementUtil.KEYWORD.EQUAL, courseID);
		StatementUtil.andWhere(statementDTO, CoursesectionDTO.COLUMN_FATHER_ID, StatementUtil.KEYWORD.EQUAL, sectionID);
		CoursesectionDTO searchDTO = new CoursesectionDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		searchDTO.set_orderByCondition(CoursesectionDTO.COLUMN_ORDERBY);
		List<CoursesectionDTO> sectionDBs = queryDAO.queryForObjectList(CoursesectionDTO.SQL_SEARCH, searchDTO);
		
		if (CollectionUtil.isEmpty(sectionDBs)) {
			if(orderID != null){
				LessonInfoDTO[] lesInfoDto=getLessonArray(sectionID, orderID, courseID);
				if(lesInfoDto!=null)
					sectionInfo.setLessonInfoArray(lesInfoDto);
			}
			else{
				LessonInfoDTO[] lesInfoDto=getLessonArray(sectionID);
				if(lesInfoDto!=null)
					sectionInfo.setLessonInfoArray(lesInfoDto);
			}
			return;
		}
		
		List<SectionInfoDTO> lstSection = new ArrayList<SectionInfoDTO>();
		for(CoursesectionDTO item : sectionDBs){
			lstSection.add(convertToSectionInfo(item, orderID));
		}
		
		sectionInfo.setChildrenSectionArray(lstSection.toArray(new SectionInfoDTO[lstSection.size()]));
	}
	
	private LessonInfoDTO[] getLessonArray(Integer sectionID){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VsectionlessonDTO.COLUMN_SECTION_ID, StatementUtil.KEYWORD.EQUAL, sectionID);
		CoursesectionDTO searchDTO = new CoursesectionDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		List<VsectionlessonDTO> sectionDBs = queryDAO.queryForObjectList(VsectionlessonDTO.SQL_SEARCH, searchDTO);
		//20150324-qindong
//		if (CollectionUtil.isEmpty(sectionDBs)) {
//			createApplicationException(M10010WS);
//		}
		List<LessonInfoDTO> lstLesson = new ArrayList<LessonInfoDTO>();
		if(sectionDBs!=null){
			for(VsectionlessonDTO item : sectionDBs){
				lstLesson.add(convertToLessonInfo(item));
			}
		}else{
			return null;
		}
		
		return lstLesson.toArray(new LessonInfoDTO[lstLesson.size()]);
	}
	
	private LessonInfoDTO[] getLessonArray(Integer sectionID, Integer orderID, Integer courseID){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, VstudylessonDTO.COLUMN_SECTION_ID, StatementUtil.KEYWORD.EQUAL, sectionID);
		StatementUtil.andWhere(statementDTO, VstudylessonDTO.COLUMN_COURSE_ID, StatementUtil.KEYWORD.EQUAL, courseID);
		StatementUtil.andWhere(statementDTO, VstudylessonDTO.COLUMN_ORDER_ID, StatementUtil.KEYWORD.EQUAL, orderID);
		CoursesectionDTO searchDTO = new CoursesectionDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		List<VstudylessonDTO> sectionDBs = queryDAO.queryForObjectList(VstudylessonDTO.SQL_SEARCH, searchDTO);
		//20150324-qindong
//		if (CollectionUtil.isEmpty(sectionDBs)) {
//			createApplicationException(M10010WS);
//		}
		List<LessonInfoDTO> lstLesson = new ArrayList<LessonInfoDTO>();
		if(sectionDBs!=null){
			for(VstudylessonDTO item : sectionDBs){
				lstLesson.add(convertToLessonInfo(item));
			}
		}else{
			return null;
		}
		
		return lstLesson.toArray(new LessonInfoDTO[lstLesson.size()]);
	}
	
	private LessonInfoDTO convertToLessonInfo(VstudylessonDTO item) {
		LessonInfoDTO lessonInfoDTO = new LessonInfoDTO();
		lessonInfoDTO.setLessonid(item.getlessonid());
		lessonInfoDTO.setName(item.getname());
		lessonInfoDTO.setPath(item.getpath());
		if(item.getstate()== Integer.parseInt(CATAGORY_P_LESSON.ENABLE.toString())){
			lessonInfoDTO.setEnablePlay(true);
		}
		else{
			lessonInfoDTO.setEnablePlay(false);
		}
		
		return lessonInfoDTO;
	}
	
	private LessonInfoDTO convertToLessonInfo(VsectionlessonDTO item) {
		LessonInfoDTO lessonInfoDTO = new LessonInfoDTO();
		lessonInfoDTO.setLessonid(item.getlessonid());
		lessonInfoDTO.setName(item.getname());
		lessonInfoDTO.setPath(item.getpath());
		return lessonInfoDTO;
	}

	private SectionInfoDTO[] getSectionFatherArray(Integer courseID, Integer orderID){
		CoursesectionDTO searchDTO = new CoursesectionDTO();
		searchDTO.setcourseid(courseID);
		List<CoursesectionDTO> sectionDBs = queryDAO.queryForObjectList(SQLID.COURSE_SECTION_FATHER_SEARCH, searchDTO);
		
		if (CollectionUtil.isEmpty(sectionDBs)) {
			createApplicationException(M10010WS);
		}
		
		List<SectionInfoDTO> lstSection = new ArrayList<SectionInfoDTO>();
		for(CoursesectionDTO item : sectionDBs){
			lstSection.add(convertToSectionInfo(item, orderID));
		}
		
		return lstSection.toArray(new SectionInfoDTO[lstSection.size()]);
	}
	
	private CourseInfoDTO getCourseBase(Integer courseID){
		StatementDTO statementDTO = new StatementDTO();
		CourseInfoDTO courseInfo = null; 
		StatementUtil.andWhere(statementDTO, MstcourseDTO.COLUMN_COURSE_ID, StatementUtil.KEYWORD.EQUAL, courseID);
		MstcourseDTO searchDTO = new MstcourseDTO();
		searchDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		MstcourseDTO courseDB = queryDAO.queryForObject(MstcourseDTO.SQL_SEARCH, searchDTO);
		if(courseDB == null){
			createApplicationException(M10010WS);
		}
		
		courseInfo = convertToCourseInfo(courseDB);
		courseInfo.setPath(getFilePath(courseDB.getfileid()));
		return courseInfo;
	}
	
	private String getFilePath(Integer fileID){
		StatementDTO statementDTO = new StatementDTO();
		StatementUtil.andWhere(statementDTO, MstfileDTO.COLUMN_FILE_ID, StatementUtil.KEYWORD.EQUAL, fileID);
		MstfileDTO searchFile = new MstfileDTO();
		searchFile.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		MstfileDTO fileDB = queryDAO.queryForObject(MstfileDTO.SQL_SEARCH, searchFile);
		
		if(fileDB == null){
			createApplicationException(M10010WS);
		}
		
		return fileDB.getpath();
	}

	private CourseInfoDTO convertToCourseInfo(MstcourseDTO item) {
		CourseInfoDTO courseInfo = new CourseInfoDTO();
		courseInfo.setCourseId(item.getcourseid());
		courseInfo.setCourseStart(item.getcoursestart());
		courseInfo.setName(item.getname());
		courseInfo.setAnnotate(item.getannotate());
		courseInfo.setPrice(item.getprice());
		courseInfo.setDiscount(Math.ceil(item.getdiscount() * item.getprice() * 10)/10);
		courseInfo.setCharacteristic(item.getcharacteristic());
		courseInfo.setCourseDetail(item.getcoursedetail());
		courseInfo.setCourseEnd(item.getcourseend());
		courseInfo.setCoursePeriod(item.getcourseperiod());
		courseInfo.setCourseTarget(item.getcoursetarget());
		courseInfo.setCourseTypec(item.getcoursetypec());
		courseInfo.setCourseTypep(item.getcoursetypep());
		courseInfo.setMaterial(item.getmaterial());
		courseInfo.setState(item.getstate());
		courseInfo.setTargetStudent(item.gettargetstudent());
		courseInfo.setTeamIntroduce(item.getteamintroduce());
		return courseInfo;
	}
	
	private CourseInfoDTO convertToCourseInfo(VcourseDTO item) {
		CourseInfoDTO courseInfo = new CourseInfoDTO();
		courseInfo.setCourseId(item.getcourseid());
		courseInfo.setCourseStart(item.getcoursestart());
		courseInfo.setName(item.getname());
		courseInfo.setAnnotate(item.getannotate());
		courseInfo.setPath(item.getpath());
		courseInfo.setPrice(item.getprice());
		courseInfo.setDiscount(Math.ceil(item.getdiscount() * item.getprice() * 10)/10);
		courseInfo.setCoursePeriod(item.getcourseperiod());
		courseInfo.setCourseEnd(item.getcourseend());
		return courseInfo;
	}

	@Override
	public MstcourseInfoDTO[] getCourseForEvaluation() {
		List<MstcourseInfoDTO> courseList=new ArrayList<MstcourseInfoDTO>();
		List<MstcourseDTO> courseListDB=new ArrayList<MstcourseDTO>();
		MstcourseDTO mstcourseDTO =new MstcourseDTO();
		courseListDB=queryDAO.queryForObjectList(MstcourseDTO.SQL_SEARCH, mstcourseDTO);
		for(MstcourseDTO item : courseListDB){
			courseList.add(convertToMstcourseInfoDTO(item));
		}
		return courseList.toArray(new MstcourseInfoDTO[courseList.size()]);
	}
	private MstcourseInfoDTO convertToMstcourseInfoDTO(MstcourseDTO item) {
		MstcourseInfoDTO courseInfo = new MstcourseInfoDTO();
		courseInfo.setcourseid(item.getcourseid());
		courseInfo.setname(item.getname());
		return courseInfo;
	}

	@Override
	public void deleteCourse(String actionType, String courseSql,
			Integer courseId) {

		if(actionType.equals("single")){
			if(courseId==null){
				return ;
			}		
			VsectionidfordeleteteacherDTO vsectionidfordeleteteacherDTO=new VsectionidfordeleteteacherDTO();
			vsectionidfordeleteteacherDTO.setcourseid(courseId);
			List<VsectionidfordeleteteacherDTO> array= queryDAO.queryForObjectList(VsectionidfordeleteteacherDTO.EQUAL_SEARCH, vsectionidfordeleteteacherDTO);
			for (VsectionidfordeleteteacherDTO item : array) {
			    SectionlessonDTO sectionlessonDTO =new SectionlessonDTO();
			    sectionlessonDTO.setsectionid(item.getsectionid());
				updateDAO.update(SQLID.SECTION_LESSON_DELETE_FOR_COURSE, sectionlessonDTO);
			} 
			CoursesectionDTO coursesectionDTO=new CoursesectionDTO();
			coursesectionDTO.setcourseid(courseId);
			updateDAO.update(SQLID.COURSE_SECTION_DELETE_FOR_COURSE, coursesectionDTO);
			MstcourseDTO mstcourseDTO=new MstcourseDTO();
			mstcourseDTO.setcourseid(courseId);
			updateDAO.update(MstcourseDTO.DELETE, mstcourseDTO);
		}else if(actionType.equals("many")){
			String[] dbSqlArray=courseSql.split(",");
			for (int i = 0; i < dbSqlArray.length; i++) {
				if(StringUtil.isEmpty(dbSqlArray[i])){
					break;
				}
				
				VsectionidfordeleteteacherDTO vsectionidfordeleteteacherDTO=new VsectionidfordeleteteacherDTO();
				vsectionidfordeleteteacherDTO.setcourseid(Integer.parseInt(dbSqlArray[i]));
				List<VsectionidfordeleteteacherDTO> array= queryDAO.queryForObjectList(VsectionidfordeleteteacherDTO.EQUAL_SEARCH, vsectionidfordeleteteacherDTO);
				for (VsectionidfordeleteteacherDTO item : array) {
				    SectionlessonDTO sectionlessonDTO =new SectionlessonDTO();
				    sectionlessonDTO.setsectionid(item.getsectionid());
					updateDAO.update(SQLID.SECTION_LESSON_DELETE_FOR_COURSE, sectionlessonDTO);
				} 
				CoursesectionDTO coursesectionDTO=new CoursesectionDTO();
				coursesectionDTO.setcourseid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.update(SQLID.COURSE_SECTION_DELETE_FOR_COURSE, coursesectionDTO);
				MstcourseDTO mstcourseDTO=new MstcourseDTO();
				mstcourseDTO.setcourseid(Integer.parseInt(dbSqlArray[i]));
				updateDAO.update(MstcourseDTO.DELETE, mstcourseDTO);
				
			}
		}
		
	}
	
}
